package cn.dakaqi.web.dashboard;

import cn.dakaqi.entities.Group;
import cn.dakaqi.entities.Volunteer;
import cn.dakaqi.group.services.GroupService;
import cn.dakaqi.member.service.VolunteerService;
import cn.dakaqi.web.shiro.ShiroDbRealm;
import cn.dakaqi.utils.BaseDAO;
import cn.dakaqi.web.BaseController;
import cn.dakaqi.web.cache.PageCache;
import org.apache.commons.lang3.StringUtils;
import org.apache.shiro.SecurityUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.servlet.ModelAndView;

import javax.servlet.http.HttpServletRequest;

/**
 * Created by chunyang on 2016/5/9.
 */
@Controller
@RequestMapping(value = "/dashboard")
public class DashboardController extends BaseController
{
    @Autowired
    BaseDAO baseDAO;
    @Autowired
    VolunteerService volunteerService;

    @Autowired
    PageCache pageCache;
    @Autowired
    GroupService groupService;

    @Override
    protected ModelAndView initModelAndView(String groupCode,String view,HttpServletRequest request)
    {
        ModelMap modelMap = super.initData(groupCode,request);
        Group group = (Group)request.getSession().getAttribute("curGroup");
        if(null == group)
        {
            String empty = "dashboard/empty";
            return  new ModelAndView(empty,modelMap);
        }
        return new ModelAndView(view,modelMap);
    }
    @Override
    protected void clearModelAndView(String groupCode,HttpServletRequest request)
    {

    }
    private Group getCurGroup(HttpServletRequest request)
    {
        return ((Group)request.getSession().getAttribute("curGroup"));
    }

    @RequestMapping(method = RequestMethod.GET)
    public ModelAndView index(@RequestParam(value = "groupCode",defaultValue = "") String groupCode,HttpServletRequest request)
    {
        Volunteer volunteer = this.volunteerService.findByUserId(getCurrentUserId());
        if(StringUtils.isBlank(volunteer.getCardNO()) || volunteer.getCardNO().toLowerCase().equals("null"))
        {
            ModelMap modelMap = super.initData(groupCode,request);
            String view = "settings/certification";
            ModelAndView modelAndView = new ModelAndView(view, modelMap);
            modelAndView.addObject("volunteer", volunteer);
            modelAndView.addObject("message", "请完善个人信息");
            return modelAndView;
        }
        else
        {
            String view = "dashboard/index";
            modelAndView = initModelAndView(groupCode, view,request);
            if(StringUtils.isNotBlank(groupCode))
            {
                group = this.groupService.findByGroupCode(groupCode);
                request.getSession().setAttribute("curGroup",group);
            }
        }

        return modelAndView;
    }

    /**
     * 取出Shiro中的当前用户Id.
     */
    private long getCurrentUserId() {
        ShiroDbRealm.ShiroUser user = (ShiroDbRealm.ShiroUser) SecurityUtils.getSubject().getPrincipal();
        if(null == user)
            return 0L;

        return user.id;
    }

    /**
     * 更新Shiro中当前用户的用户名.
     */
    private void updateCurrentUserName(String userName) {
        ShiroDbRealm.ShiroUser user = (ShiroDbRealm.ShiroUser) SecurityUtils.getSubject().getPrincipal();
        user.realName = userName;
    }
}
